#!/bin/bash

set -o errexit
set -o xtrace

test_dir=$(realpath $(dirname $0))
. ${test_dir}/../functions
cluster="no-limits"

pumba_pod() {
    local action=$1
    local pod=$(get_operator_pod)
 
    if [ -n "$OPERATOR_NS" ]; then
        kubectl_bin config set-context $(kubectl_bin config current-context) --namespace="$OPERATOR_NS"
    fi
    # run chaos for Pod
    run_pumba $action "re2:$pod"
    sleep 2

    wait_pod $pod
    kubectl_bin config set-context $(kubectl_bin config current-context) --namespace="$namespace"
}

pause_pod() {
    local pod=$(get_operator_pod)

    if [ -n "$OPERATOR_NS" ]; then
        kubectl_bin config set-context $(kubectl_bin config current-context) --namespace="$OPERATOR_NS"
    fi
    # run chaos for Pod
    kubectl_bin exec ${OPERATOR_NS:+-n $OPERATOR_NS} -it "$(get_pumba)" -- /pumba -l info pause --duration 1m "re2:$pod" &
    pid=$!
    sleep 10
    kubectl_bin config set-context $(kubectl_bin config current-context) --namespace="$namespace"

    desc 'check if operator works fine: scale down from 4 to 3'
    cat_config "$test_dir/conf/$cluster.yml" \
        | sed -e 's/size: 4/size: 3/' \
        | kubectl_bin apply -f-

    # wait for unpause
    wait $pid

    if [ -n "$OPERATOR_NS" ]; then
        kubectl_bin config set-context $(kubectl_bin config current-context) --namespace="$OPERATOR_NS"
    fi
    # check if Pod started
    wait_pod $pod
    kubectl_bin config set-context $(kubectl_bin config current-context) --namespace="$namespace"

    # check scale down
    wait_for_delete pod/$cluster-pxc-3
}

netem_pod() {
    local pod=$(get_operator_pod)

    if [ -n "$OPERATOR_NS" ]; then
        kubectl_bin config set-context $(kubectl_bin config current-context) --namespace="$OPERATOR_NS"
    fi
    # run chaos for Pod
    kubectl_bin exec ${OPERATOR_NS:+-n $OPERATOR_NS} -it "$(get_pumba)" -- /pumba -l info netem --tc-image 'gaiadocker/iproute2' --duration 1m loss --percent 100 --correlation 100 "re2:$pod" &
    pid=$!
    sleep 10
    kubectl_bin config set-context $(kubectl_bin config current-context) --namespace="$namespace"

    desc 'check if operator works fine: scale up from 3 to 4'
    cat_config "$test_dir/conf/$cluster.yml" \
        | sed -e 's/size: 3/size: 4/' \
        | kubectl_bin apply -f-

    # wait for unpause
    wait $pid

    if [ -n "$OPERATOR_NS" ]; then
        kubectl_bin config set-context $(kubectl_bin config current-context) --namespace="$OPERATOR_NS"
    fi
    # check if Pod started
    wait_pod $pod
    kubectl_bin config set-context $(kubectl_bin config current-context) --namespace="$namespace"

    # check scale up
    wait_for_running "$cluster-pxc" 4
}


main() {
    create_infra $namespace

    if [ -n "$OPERATOR_NS" ]; then
        kubectl_bin patch clusterrole percona-xtradb-cluster-operator --type=json -p '[{"op":"remove","path":"/rules/1"}]'
        kubectl_bin delete validatingwebhookconfigurations.admissionregistration.k8s.io percona-xtradbcluster-webhook
    fi

    kubectl_bin apply -f "$test_dir/conf/pumba.yml" ${OPERATOR_NS:+-n $OPERATOR_NS}

    desc 'create PXC cluster'
    spinup_pxc "$cluster" "$test_dir/conf/$cluster.yml"

    desc 'kill operator'
    pumba_pod kill
    desc 'check if operator works fine: scale up from 3 to 4'
    cat_config "$test_dir/conf/$cluster.yml" \
        | sed -e 's/size: 3/size: 4/' \
        | kubectl_bin apply -f-
    wait_for_running "$cluster-pxc" 4

    desc 'rm operator'
    pumba_pod rm
    desc 'check if operator works fine: scale down from 4 to 3'
    cat_config "$test_dir/conf/$cluster.yml" \
        | sed -e 's/size: 4/size: 3/' \
        | kubectl_bin apply -f-
    wait_for_delete pod/$cluster-pxc-3

    desc 'stop operator'
    pumba_pod "stop -t 10"
    desc 'check if operator works fine: scale up from 3 to 4'
    cat_config "$test_dir/conf/$cluster.yml" \
        | sed -e 's/size: 3/size: 4/' \
        | kubectl_bin apply -f-
    wait_for_running "$cluster-pxc" 4

    desc 'pause operator'
    pause_pod

    desc 'emulate bad network'
    netem_pod

    destroy $namespace
}

main
